Intersoft ClientUI 8 > ClientUI Fundamentals > Application Framework Overview > Application Framework How-to Topics > How-to: Implement IShellManager to Handle Application-wide Events |
The following example shows how to create a class that implements IShellManager, then assign an instance of the class to the UXShell to receive application-wide events.
Since UXShell is modeled based on shell pattern, it implements low level architecture that propagates application-wide events such as Downloading, Downloaded, Installed, Launched, NetworkOnline and more. Many of these methods are abstracted into IShellManager interface to allow loosely-coupled application design, which separates the actual implementation logic that handles the application events from the ClientUI application model.
To handle the application-wide events in UXShell, you create a class that implements IShellManager interface, then instantiate a new object of that particular class and bind it to the ShellUIManager property of the shell.
The following example code defines the class that implements the IShellManager interface. In this example, only the Downloading and Downloaded are implemented.
MyShellManager.cs |
Copy Code
|
---|---|
using System.Threading; using Intersoft.Client.Framework; namespace ClientUIApplication_Docs.AppFramework { public class MyShellManager : IShellManager { public UXPage1 RootPage { get; set; } #region IShellManager Members public void Closed(ApplicationActivityEventArgs arg) { // write your logic here to handle application closed event } public void Closing(ApplicationActivityEventArgs arg) { // write your logic here to handle application closing event } public void Connecting(ApplicationActivityEventArgs arg) { // write your logic here to handle application connecting event } public void DownloadCancelled(ApplicationActivityEventArgs arg) { // write your logic here to handle download cancelled event } public void DownloadFailed(ApplicationActivityEventArgs arg) { // write your logic here to handle download failed event } public void Downloaded(ApplicationActivityEventArgs arg) { // set the status label to indicate the current operation this.RootPage.StatusLabel.Content = arg.Application.Name + " was successfully completed."; } public void Downloading(ApplicationActivityEventArgs arg) { // set the status label to indicate the current operation this.RootPage.StatusLabel.Content = "Downloading " + arg.Application.Name; // set the progress bar value to the current download progress this.RootPage.DownloadProgressBar.Value = ((ApplicationDownloadingEventArgs)arg.ActivityDetails).TotalPercentCompleted; // simulate server callback Thread.Sleep(200); } public void InstallFailed(ApplicationActivityEventArgs arg) { // write your logic here to handle install failed event } public void Installed(ApplicationActivityEventArgs arg) { // write your logic here to handle application installed event } public void Installing(ApplicationActivityEventArgs arg) { // write your logic here to handle application installing event } public void LaunchFailed(ApplicationActivityEventArgs arg) { // write your logic here to handle launch failed event } public void Launched(ApplicationActivityEventArgs arg) { // write your logic here to handle application launched event } public void Launching(ApplicationActivityEventArgs arg) { // write your logic here to handle application launching event } public void NetworkOffline(NetworkConnectivityEventArgs arg) { // write your logic here to handle network offline event } public void NetworkOnline(NetworkConnectivityEventArgs arg) { // write your logic here to handle network online event } public void ScreenStateChanged(ScreenStateChangedEventArgs arg) { // write your logic here to handle screen state changed event } public void Synchronized(SystemEventArgs arg) { // write your logic here to handle application synchronized event } public void Synchronizing(SystemEventArgs arg) { // write your logic here to handle application synchronizing event } public void Uninstalled(ApplicationActivityEventArgs arg) { // write your logic here to handle application uninstalled event } public void Uninstalling(ApplicationActivityEventArgs arg) { // write your logic here to handle application uninstalling event } #endregion } } |
The following example code assigns an instance of the class to the ShellUIManager property of the shell object which is typically configured in the code behind of the App.xaml.
App.xaml.cs |
Copy Code
|
---|---|
... // define the ApplicationID used to reference this application from UXShell or other containers public static string ApplicationID = "ClientUIApplication_Docs"; public App() { this.Startup += this.Application_Startup; InitializeComponent(); InitializeShell(); } private void InitializeShell() { AppFramework.MyShellManager shellManager = new AppFramework.MyShellManager(); shellManager.RootPage = new AppFramework.UXPage1(); UXShell shell = new UXShell(); shell.RootApplication = UXShell.CreateApplicationFromType(typeof(App), ApplicationID, ApplicationID); shell.ShellUIManager = shellManager; this.ApplicationLifetimeObjects.Add(shell); } private void Application_Startup(object sender, StartupEventArgs e) { this.RootVisual = ((AppFramework.MyShellManager)UXShell.Current.ShellUIManager).RootPage; } |
The following example code shows the XAML page that is used in the MyShellManager class shown in the above example, which acts as the mediator that connects the application events to the user interface.
UXPage1.xaml |
Copy Code
|
---|---|
<Intersoft:UXPage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:Intersoft="http://intersoft.clientui.com/schemas" x:Class="ClientUIApplication_Docs.AppFramework.UXPage1" Title="UXPage1 Page" d:DesignWidth="640" d:DesignHeight="480"> <Grid x:Name="LayoutRoot"> <StackPanel Orientation="Vertical" VerticalAlignment="Center"> <Intersoft:StylishLabel Content="Status" HorizontalAlignment="Center" Name="StatusLabel" VerticalAlignment="Top" /> <Intersoft:UXProgressBar Height="25" HorizontalAlignment="Center" Name="DownloadProgressBar" Width="305" Margin="10"/> <Intersoft:UXButton Content="Download" HorizontalAlignment="Center" Name="DownloadButton" Width="100" Click="DownloadButton_Click" /> </StackPanel> </Grid> </Intersoft:UXPage> |
In the page, the Download button creates a new ApplicationPackage and download the package from the specified Source, which is explained in the following code. This example presumes that you have a ClientUI application named ExternalClientUIApp1.xap in the ClientBin folder of the Web project where the root application existed.
UXPage1.xaml.cs |
Copy Code
|
---|---|
using System; using System.Windows; using Intersoft.Client.Framework; using Intersoft.Client.UI.Navigation; namespace ClientUIApplication_Docs.AppFramework { public partial class UXPage1 : UXPage { public UXPage1() { InitializeComponent(); } // Executes when the user navigates to this page. protected override void OnNavigatedTo(NavigationEventArgs e) { } private void DownloadButton_Click(object sender, RoutedEventArgs e) { // create a new ApplicationPackage that represents ExternalClientUIApp1 ApplicationPackage app = new ApplicationPackage() { ID = "ExternalClientUIApp1", Name = "ExternalClientUIApp1", Source = new Uri("ExternalClientUIApp1.xap", UriKind.RelativeOrAbsolute), Size = -1 // auto detect the file size at runtime }; // add the application to the UXShell UXShell.Current.Applications.Add(app); // download the application app.Download(false); } } } |
When the button is clicked, you can notice that the progress bar and status label displays the current download progress in real time. The result is shown in the following figure.